import os    
from eyeD3 import *
from shutil import copyfile

#TODO: add cmdline options
#TODO: inputsource filelist, or dirname to walk thru recursively



def getfilelist(dirname,ext=''):
""" returns recursive filelist tuple for given extension """
#TODO: create tests for getfilelist

    filelist=[]
    for root,dirs,files in (os.walk(dirname)):      
        for f in files :
            filepath= root+os.sep+f
            if (len(f)-len(ext)==f.find(ext)):
                filelist.append(os.path.abspath(filepath))    
    return filelist


def has_suffix(s,suffix):
    """does that string has suffix?"""
    return s.find(suffix)+len(suffix)==len(s)

def is_clone(fname1,fname2):
    return os.path.split(fname1)[1]==os.path.split(fname2)[1]


def cpID3(fsrc,fdst):
    src_tag= eyeD3.Tag()
    src_tag.link(fsrc,eyeD3.ID3_V2)
    dst_tag= eyeD3.Tag()
    dst_tag.link(fdst)
    dst_tag.frames=src_tag.frames
    dst_tag.update(eyeD3.ID3_V2_3)

def mkdummies(src,dst,flist):    
    src=os.path.realpath(src)
    dst=os.path.realpath(dst)
    if not has_suffix(dst,os.sep): dst +=os.sep #make sure to add os.sep suffix
    for f in flist:                
        fdest=f.replace(src,dst)
	print f, src, dst , "/ncopying to", fdest
        fpath=os.path.split(fdest)[0]
        if not os.path.exists(fpath): 
         os.makedirs(fpath)                     
        print dummy_mp3,fpath
        copyfile(dummy_mp3,fdest)        
	cpID3(f,fdest)
        if not os.path.exists(fdest):
            raise fdest, "was not copied"
            
dummy_mp3=os.path.realpath('./0_0_10.mp3')
src_list=getfilelist('m:/kw','.mp3')
src_base='m:/'
dst_base='m:/kwtags/'

#mkdestdirs(src_base,dst_base,src_list)    
mkdummies(src_base,dst_base,src_list)
